package com.zj.core.dialog

import android.app.Activity
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.LinearLayout
import com.fileutils.BaseConstant
import com.fileutils.NumberUtils
import com.jniutils.DataCollUtil
import com.utils.DateUtils
import com.zj.core.R
import com.zj.core.base.BaseDialog
import com.zj.core.interfaces.TextMessageIml
import com.zj.core.tools.ViewPosition
import com.zj.core.tools.VoiceAsrTools
import com.zj.core.ui.SearchDialogUi
import java.lang.StringBuilder

class SearchDialog(activity: Activity?,noteIml: TextMessageIml?) : BaseDialog(activity), View.OnClickListener,
    TextMessageIml {
    private var mActivity: Activity?=activity
    private var mCallIml: TextMessageIml?=noteIml
    private var noteDialogView: SearchDialogUi?=null
    private var mBuilder: StringBuilder?=null
    private var mHandle:VoiceAsrTools?=null
    private var mSwitchTime=""                         //多时间处理

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val width: Int = ViewPosition().getScreenWidth(this.context)
        val height: Int = ViewPosition().getScreenHeight(this.context)
        val params = LinearLayout.LayoutParams(width, height, 0f)
        noteDialogView= SearchDialogUi(context)
        noteDialogView?.apply {
            setContentView(common_dialog_root_view,params)
            mBuilder= StringBuilder()
            //setCancelable(cancelable)
            setCanceledOnTouchOutside(true)
            search_voice?.setOnClickListener(this@SearchDialog)
            common_dialog_root_view?.setOnClickListener(this@SearchDialog)
            common_dialog_message_tv?.setSelection(common_dialog_message_tv?.text.toString().length)
            guide_line_lly?.setOnClickListener(this@SearchDialog)
        }
        mHandle= VoiceAsrTools().setVoiceImage(noteDialogView?.voice_img_view).setHandleListener(this)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        mHandle?.onKeyDown(keyCode,mActivity,noteDialogView?.common_dialog_message_tv?.windowToken)
        return true
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
        mHandle?.onKeyUp(keyCode,mActivity)
        return false
    }

    override fun messageResult(msg: String) {
        noteDialogView?.common_dialog_message_tv?.apply {
            if (moreTimeSwitch(msg)){
                setText(mSwitchTime)
                setSelection(mSwitchTime.length)
            }else{
                val bedNum=specificVoice(msg)
                setText(bedNum)
                setSelection(bedNum.length)
            }
        }
    }

    /**
     * 多时间转化
     */
    private fun moreTimeSwitch(time:String):Boolean{
        var time1=""
        var time2=""
        if(time.contains("到")){
            val timeSplit=time.split("到")

            if(timeSplit[0]!=""){
                time1=DateUtils.dateFormat(0,timeSplit[0],"").split("|")[0]
            }

            if(timeSplit[1]!=""){
                time2=DateUtils.dateFormat(0,timeSplit[1],"").split("|")[0]
            }

            if(time1!=""&&DateUtils.isValidDate(time1, "HH:mm")
                &&time2!=""&&DateUtils.isValidDate(time2, "HH:mm")){
                mSwitchTime="$time1-$time2"
                return true
            }
        }else{
            val formatData = DateUtils.dateFormat(0,time,"").split("|")[0]
            if (formatData != "" && DateUtils.isValidDate(formatData, "HH:mm")){
                mSwitchTime=formatData
                return true
            }
        }
        return false
    }

    /**
     * 床号处理
     */
    private fun specificVoice(str:String):String{
        if(str=="起床"){
            return "7床"
        }

        val bedValue= DataCollUtil.patternValue(BaseConstant.PATTERN_BED,str)
        mBuilder?.clear()

        if(bedValue!=null&&bedValue!=""&&bedValue!="床"){
            val index=str.indexOf(bedValue)
            mBuilder?.append(str.replace(bedValue,""))
            return mBuilder?.insert(index, NumberUtils.CharDateFormat(bedValue)).toString()
        }
        return str
    }

    override fun onClick(v: View?){
        when(v?.id){
            R.id.search_voice->{
                if(noteDialogView?.common_dialog_message_tv?.text.toString()!=""){
                    if(mCallIml!=null){
                        mCallIml?.messageResult(noteDialogView?.common_dialog_message_tv?.text.toString())
                    }
                    dismiss()
                }
            }

            R.id.parent_cs_layout->{
                dismiss()
            }
        }
    }
}